package fibonacci;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Sample extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JPanel pApp, pTop, pMiddle, pBottom;
	private JTextField tQtde;
	private JTextArea taResult;
	private JButton bCalc;

	public Sample() {
		super("Número de Fibonacci");
		this.setVisible(true);
		this.setResizable(false);

		pApp = new JPanel();
		pApp.setLayout(new BorderLayout());

		pTop = new JPanel();
		pTop.setLayout(new FlowLayout());
		pTop.setBorder(BorderFactory.createTitledBorder("Quantidade"));

		pMiddle = new JPanel();
		pMiddle.setLayout(new FlowLayout());
		pMiddle.setBorder(BorderFactory.createTitledBorder("Resultado"));

		pBottom = new JPanel();
		pBottom.setLayout(new FlowLayout());

		tQtde = new JTextField("20", 5);

		taResult = new JTextArea(10, 50);

		bCalc = new JButton("Calcular");
		bCalc.addActionListener(this);

		pTop.add(tQtde);

		pMiddle.add(new JScrollPane(taResult));

		pBottom.add(bCalc);

		pApp.add(pTop, BorderLayout.PAGE_START);
		pApp.add(pMiddle, BorderLayout.CENTER);
		pApp.add(pBottom, BorderLayout.PAGE_END);

		add(pApp);

		this.pack();
	}

	public void actionPerformed(ActionEvent e) {
		String evento = e.getActionCommand();

		if (evento == "Calcular") {
			calcFibonacci(Integer.parseInt(tQtde.getText()), 0, 1);
		}
	}

	public int calcFibonacci(int n, int a, int b) {
		String resultado = "";
		
		resultado = taResult.getText() + " " + String.valueOf(a);
		
		taResult.setText(resultado);

		return (n == 0) ? a : calcFibonacci(n - 1, a + b, a);
	}

	public static void main(String[] args) {
		new Sample();
	}
}
